package com.meichis.mcsappframework.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

import com.meichis.mcsappframework.R;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class DropDownTabDialog<T> extends Dialog implements View.OnClickListener {


    private Context mContext;
    private List<T> mList = new ArrayList<>();
    private List<T> tempList = null;
    private RelativeLayout rlDialog;
    private ImageButton btnOK;
    private TextView tvTab1;
    private TextView tvTab2;
    private ListView lv;
    private TextView txtTitle;
    private EditText etSearch;

    private String title;
    private boolean isFilter = false;
    //dialog弹出框的背景颜色
    private int rlColorBg = R.color.blue;
    private String TextData;
    private boolean Cancelable = true;
    private boolean isSingleLine = true;
    private int textStatus;
    ItemAddAdapter adapter;
    private SelectedListener selectedListener;
    private String tab1;
    private String tab2;
    private List<T> tabList1;
    private List<T> tabList2;


    public DropDownTabDialog(Context context, int colorBgId,
                             String Title, String dTextData,
                             String Tab1, List<T> TabList1,
                             String Tab2, List<T> TabList2,
                             SelectedListener selectlistener) {
        super(context);
        mContext = context;
        title = Title;
        tab1 = Tab1;
        tab2 = Tab2;
        tabList1 = TabList1;
        tabList2 = TabList2;
        TextData = dTextData;
        this.selectedListener = selectlistener;
        rlColorBg = colorBgId;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_choice_tab);
        findViewById();
        try {
            this.dismiss();
        } catch (Exception e) {
        }
        initData();
    }

    //初始化控件
    private void findViewById() {
        txtTitle = findViewById(R.id.txtTitle);
        rlDialog = findViewById(R.id.rl_dialog);
        btnOK = findViewById(R.id.btnCancel);
        tvTab1 = findViewById(R.id.tv_tab1);
        tvTab2 = findViewById(R.id.tv_tab2);
        lv = findViewById(R.id.lst);
        if (isFilter) {
            etSearch = findViewById(R.id.et_search);
            etSearch.addTextChangedListener(etWatcher);
            etSearch.setVisibility(View.VISIBLE);
        }

        tvTab1.setOnClickListener(this);
        tvTab2.setOnClickListener(this);
    }

    //初始化数据
    @SuppressLint("ResourceAsColor")
    private void initData() {
        rlDialog.setBackgroundResource(rlColorBg);
        setTitle(title);
        setCancelable(Cancelable);

        tvTab1.setText(tab1);
        tvTab2.setText(tab2);
        mList.addAll(tabList1);
        tvTab1.setTextColor(rlColorBg);

        adapter = new ItemAddAdapter();
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                selectedListener.setSelect(mList, position);
                DropDownTabDialog.this.dismiss();
            }
        });

        btnOK.setVisibility(Cancelable ? View.VISIBLE : View.GONE);
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                DropDownTabDialog.this.dismiss();
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        lv.setSelection(0);
    }


    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_tab1) {
            mList.clear();
            mList.addAll(tabList1);
            tvTab1.setTextColor(rlColorBg);
            tvTab2.setTextColor(R.color.dark_gray);
        } else if (i == R.id.tv_tab2) {
            mList.clear();
            mList.addAll(tabList2);
            tvTab1.setTextColor(R.color.dark_gray);
            tvTab2.setTextColor(rlColorBg);
        }

        notifyDataChanged();
    }


    TextWatcher etWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (isFilter) {
                mList = tempList;
                ArrayList<T> selectorList = new ArrayList<>();
                for (int i = 0; i < mList.size(); i++) {
                    if (mList.get(i) instanceof CharSequence) {
                        if (mList.get(i).toString().contains(s)) {
                            selectorList.add(mList.get(i));
                        }
                    } else {
                        try {
                            Class<? extends Object> objClazz = mList.get(i).getClass();
                            Method fieldGetMet;
                            fieldGetMet = objClazz.getMethod("get" + TextData, new Class[]{});

                            Object fieldVal = fieldGetMet.invoke(mList.get(i), new Object[]{});
                            if (fieldVal != null) {
                                if (String.valueOf(fieldVal).contains(s)) {
                                    selectorList.add(mList.get(i));
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                mList = selectorList;
                notifyDataChanged();
            }
        }
    };

    public void notifyDataChanged() {
        adapter.notifyDataSetChanged();
    }


    public class ItemAddAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            if (mList != null) {
                return mList.size();
            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int position) {
            if (mList != null) {
                return mList.get(position);
            } else {
                return null;
            }
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            String item = "";
            //字符串
            if (mList.get(position) instanceof CharSequence) {
                item = mList.get(position).toString();
            } else {//实体类
                Class<? extends Object> objClazz = mList.get(position).getClass();
                Method fieldGetMet;
                try {
                    fieldGetMet = objClazz.getMethod("get" + TextData, new Class[]{});

                    Object fieldVal = fieldGetMet.invoke(mList.get(position), new Object[]{});
                    if (fieldVal != null) {
                        item = String.valueOf(fieldVal);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            TextView textView = null;
            if (convertView == null) {
                textView = new TextView(mContext);
                textView.setPadding(20, 20, 20, 20);
                textView.setTextSize(18);
                textView.setSingleLine(isSingleLine);
                ColorStateList cls = null;
                cls = (ColorStateList) mContext.getResources().getColorStateList(R.color.selector_popview_light);
                textView.setTextColor(cls);
            } else {
                textView = (TextView) convertView;
            }
            textView.setText(item);
            return textView;
        }
    }

    public void setTextData(String value) {
        TextData = value;
    }

    @Override
    public void setTitle(CharSequence title) {
        // TODO Auto-generated method stub
        txtTitle.setText(title);
    }


    public interface SelectedListener {

        void setSelect(Object o, int position);
    }


}
